import matplotlib.pyplot as plt
import numpy as np
from matplotlib import style
import matplotlib as mpl

# 设置风格
plt.style.use('seaborn-v0_8-pastel')

# 数据准备
models = ['QwQ-32B', 'Deepseek-32B']
tasks = ['立法权限冲突', '奖惩冲突', '规范检查', '部门职责分析', '流程提取任务']
data = {
    'QwQ-32B': [0.494, 0.391, 0.285, 0.623, 0.67],
    'Deepseek-32B': [0.474, 0.337, 0.247, 0.566, 0.75]
}

# 设置支持中文的字体
plt.rcParams['font.sans-serif'] = ['SimHei']  # 使用黑体
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

# 创建图形
fig, ax = plt.subplots(figsize=(14, 7))
fig.patch.set_facecolor('#f8f9fa')  # 设置图表背景色
ax.set_facecolor('#f8f9fa')  # 设置坐标区背景色

# 设置柱状图位置
x = np.arange(len(tasks))
width = 0.3  # 使柱状图窄一些，更美观

# 定义颜色 - 更高级的配色
colors = ['#5470c6', '#91cc75']  # 优雅的蓝绿配色

# 绘制柱状图
bars1 = ax.bar(x - width/2, data['QwQ-32B'], width, label='QwQ-32B', color=colors[0], alpha=0.85, edgecolor='white', linewidth=0.7)
bars2 = ax.bar(x + width/2, data['Deepseek-32B'], width, label='Deepseek-32B', color=colors[1], alpha=0.85, edgecolor='white', linewidth=0.7)

# 添加数据标签
def add_labels(bars):
    for bar in bars:
        height = bar.get_height()
        ax.annotate(f'{height:.3f}',
                    xy=(bar.get_x() + bar.get_width() / 2, height),
                    xytext=(0, 5),  # 5 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom', fontsize=10, fontweight='bold')

add_labels(bars1)
add_labels(bars2)

# 设置图形标题和标签
ax.set_xlabel('任务类型', fontsize=13, fontweight='bold')
ax.set_ylabel('数值', fontsize=13, fontweight='bold')
ax.set_title('模型性能对比', fontsize=18, fontweight='bold', pad=20)

# 设置x轴刻度
ax.set_xticks(x)
ax.set_xticklabels(tasks, fontsize=11)

# 添加图例
ax.legend(loc='upper left', fontsize=12, frameon=True, facecolor='white', edgecolor='lightgray')

# 添加网格线
ax.grid(axis='y', linestyle='--', alpha=0.2, color='gray')
ax.spines['top'].set_visible(False)  # 移除上边框
ax.spines['right'].set_visible(False)  # 移除右边框
ax.spines['left'].set_color('#cccccc')  # 更改左边框颜色
ax.spines['bottom'].set_color('#cccccc')  # 更改底边框颜色

# 调整布局
plt.tight_layout()

# 保存高质量图片
plt.savefig('模型性能对比.png', dpi=600, bbox_inches='tight', facecolor=fig.get_facecolor())
plt.savefig('模型性能对比.pdf', format='pdf', bbox_inches='tight', facecolor=fig.get_facecolor())

# 显示图形
plt.show()